package gw.com.android.ui.record;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import java.util.Locale;

/**
 * Created by reeta.zou on 2018/3/14.
 * 视频录制上传进度处理
 */
public class RecordLoading extends Dialog {

    protected FragmentActivity mOwnerAct;
    private ImageView mImageView;
    private TextView mTextView;

    /**
     * 默认有提示信息
     */
    RecordLoading(Activity activity) {
        super(activity, R.style.dialog_loading_bar_no_frame);
        mOwnerAct = (FragmentActivity) activity;
        initParam();
    }

    @Override
    protected void onStart() {
        getWindow().setBackgroundDrawable(new ColorDrawable(0x33000000));
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }

    public void initParam() {
        View rootView = getLayoutInflater().inflate(R.layout.dialog_record_loading, null, false);
        inflaterCustomView(rootView);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        /* The next line will add the ProgressBar to the dialog. */
        addContentView(rootView, lp);
    }

    public void inflaterCustomView(View mCustomView) {
        mCustomView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mImageView = (ImageView) mCustomView.findViewById(R.id.loading_imageview);
        mTextView = (TextView) mCustomView.findViewById(R.id.text_view);
        Glide.with(mOwnerAct).load(R.mipmap.loading).into(mImageView);
    }

    /**
     * 修改进度条
     */
    public void setProgress(int progress) {
        String text = getContext().getResources().getString(R.string.record_upload_progress);
        text = String.format(Locale.getDefault(), text, progress + "%");
        mTextView.setText(text);
    }

    /**
     * 失败隐藏view
     */
    public void failView() {
        mImageView.setVisibility(View.GONE);
        mTextView.setVisibility(View.GONE);
    }


}
